data <- read.csv("USA_GDP_Annual.csv")
Note: All values are measured in millions of USD.
# remove commas & convert to numeric
data$GDP <- as.numeric(gsub("," ,"" , data$GDP))
data$Consumption <- as.numeric(gsub("," ,"" , data$Consumption))
data$Investment <- as.numeric(gsub("," ,"" , data$Investment))
data$Net.Exports <- as.numeric(gsub("," ,"" , data$Net.Exports))
data$Government.Spending <- as.numeric(gsub("," ,"" , data$Government.Spending))
# keep years that are multiples of 10
data10 <- subset(data, Year %% 10 == 0)
# add proportion of consumption, investment, net exports, and government spending
data10 <- data10 %>% mutate(
Consumption.prop = Consumption / GDP,
Investment.prop = Investment / GDP,
Net.Exports.prop = Net.Exports / GDP,
Government.Spending.prop = Government.Spending / GDP
)